import { getQr } from '../../api/index.js'

// pages/recommend/recommend.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    qrSrc: '',
    active: false
  },

  onShow: function() {
    this.showTip()
    this.getQr()
  },

  getQr: function() {
    getQr()
    .then(res => {
      this.setData({
        qrSrc: `https://www.kecc.ltd/${res.data.url}`
      })
    })
    .catch(e => {
      console.log(e, '获取二维码')
    })
  },

  navigate: function() {
    wx.navigateTo({
      url: '/pages/recommendDetail/recommendDetail',
    })
  },

  onShareAppMessage: function(e) {
    console.log(e,'分型')
  },

  // onShareAppMessage: function() {
  //   wx.onShareAppMessage({
  //     title: '科彩运营管理',
  //     path: '/pages/index/index',
  //     imageUrl: this.data.qrSrc
  //   })
  // }

  showTip: function() {
    const active = wx.getStorageSync('active')

    this.setData({
      active
    })
  },

  saveImg: function() {
    const that = this
    const {qrSrc} = this.data

    if (!qrSrc) {
      return
    }

    wx.downloadFile({
      url: qrSrc,
      success: (downFile) => {
        wx.saveImageToPhotosAlbum({
          filePath: downFile.tempFilePath,
          success: (res) => {
            console.log(res, '保存图片成功')
            wx.showToast({
              title: '保存成功'
            })
          },
          fail: (e) => {
            console.log(e, this.data, '保存图片失败')
            wx.showToast({
              title: '保存图片失败',
              icon: 'none'
            })
          }
        })
      }
    })

    
  }
})